<?php

class Cabang extends Controller {

/**vate $limit = 10;
	/**
	 * Constructor
	 */
	function Cabang()
	{
		parent::Controller();
		$this->load->model('Cabang_model', '', TRUE);
		$this->load->library('table');
		$this->load->helper('url');
	}
	
	function index($offset = 0){
        // offset
        $uri_segment = 3;
        $offset = $this->uri->segment($uri_segment);
         
        // load data
        $cabang = $this->Cabang_model->get_paged_list($this->limit, $offset)->result()
		
         
        // generate pagination
        $this->load->library('pagination');
        $config['base_url'] = site_url('cabang');
        $config['total_rows'] = $this->Cabang_model->count_all();
        $config['per_page'] = $this->limit;
        $config['uri_segment'] = $uri_segment;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
         
        // generate table data
        $this->load->library('table');
        $this->table->set_empty("&nbsp;");
        $this->table->set_heading('No', 'Nama Cabang', 'Kota','Actions');
        $i = 0 + $offset;
        foreach ($cabangs as $cabang){
            $this->table->add_row(++$i,$cabang->cabang, kota)
                anchor('cabang/view/'.$cabang->id_cabang,'view',array('class'=>'view')).' '.
                anchor('cabang/update/'.$cabang->id_cabang,'update',array('class'=>'update')).' '.
                anchor('cabang/delete/'.$cabang->id_cabang,'delete',array('class'=>'delete','onclick'=>"return confirm('Apakah anda yakin ingin menghapus cabang ini?')"))
            );
        }
        $data['table'] = $this->table->generate();
         
        // load view
        $this->load->view('cabang',$data);
    }
     
	 function add(){
        // set validation properties
        $this->_set_fields();
         
        // set common properties
        $data['title'] = 'Tambah Cabang Baru';
        $data['message'] = '';
        $data['action'] = site_url('cabang/add');
        $data['link_back'] = anchor('cabang','kembali ke daftar cabang',array('class'=>'back'));
     
        // load view
        $this->load->view('cabang_form',$data);
    }
	
	 function addCabang(){
        // set common properties
        $data['title'] = 'Tambah cabang baru';
        $data['action'] = site_url('cabang/addproses');
        $data['link_back'] = anchor('cabang','kembali ke daftar cabang',array('class'=>'back'));
        $this->_set_fields();
        $this->_set_rules();
       //run validation
	   if ($this->validation->run() == FALSE{
	   $data[message] = '';
	   }else{
	   //save data
	$cabang = array('cabang'		=> $this->input->post('cabang'),
							'alamat'		=> $this->input->post('alamat'),
							'kota'			=> $this->input->post('kota'),
							'phone'			=> $this->input->post('phone'),
							'fax'			=> $this->input->post('fax'),
							'email'			=> $this->input->post('email')
							$id = $this->Cabang_model->id = $id_cabang;
            // set form input name="id"
            $this->validation->id = $id;
             
            // set user message
            $data['message'] = '<div class="success">add new person success</div>';
        }
         
        // load view
        $this->load->view('cabang',$data);
    }
	
	function view($id){
	//set common properties
	$data['tittle'] = 'Detail Cabang';
	$data['link_back'] = anchor('cabang','kembali',array('class'=>'back'));
	//get cabang detail
	$data['cabang'] = $this->Cabang_model->get_cabang_by_id($id)->row();
	//load view
	$this->load->view('cabang_view',$data);
	}
	function update($id){
	//set validation properties
	$this->_set_fields();
	
	$cabang = $this->Cabang_model->get_cabang_by_id($id_cabang)->row();
	$this->validation->caband= $cabang->cabang;
	$this->vallidation->Alamat= $cabang->alamat;
	$this->validation->kota=$cabang->kota;
	$this->validation->fax=$cabang->fax;
	$thia->validation->email=$cabang->email;
	
	$data['title'] = 'Update Cabang';
        $data['message'] = '';
        $data['action'] = site_url('cabang'/'update');
        $data['link_back'] = anchor('cabang','kembali',array('class'=>'back'));
     
        // load view
        $this->load->view('cabang_form',$data);
}
 function updateprocess(){
 $data['tittle'] = 'Update data cabang',$data;
 $data['action'] = site_url(cabang/updatepros);
  $data['link_back'] = anchor('cabang','kembali',array('class'=>'back'));
  
  //set validation properties
 $this->_set_fields();
 $this->_set_rules();
 
 if($this->validation->run()==FALSE{
 $data['message']='';
 }else{
 //save data
 $id_cabang = $this->input->post('id_cabang');
 $cabang = array('cabang'		=> $this->input->post('cabang'),
							'alamat'		=> $this->input->post('alamat'),
							'kota'			=> $this->input->post('kota'),
							'phone'			=> $this->input->post('phone'),
							'fax'			=> $this->input->post('fax'),
							'email'			=> $this->input->post('email')
							$this->Cabang_model->update($id_cabang,$cabang);
							
							$data[message] = '<div class="success">Satu data cabang berhasil diubah</div>'
 }
 //load view
 $this->load->view('cabang',$data);
 }
 
 function delete($id_cabang){
 $this->Cabang_model->delete($id_cabang);
 redirect('cabang','refresh');
 }
 
 function _set_fields(){
 $fields['cabang']='cabang';
 $fields['alamat']='alamat';
 $fields['kota']='kota';
 $fields['phone']='phone';
 $fields['fax']='fax';
 $fields['email']='email';
 
 $this->validation->set_fields($fields);
 
 function _set_rules(){
  $fields['cabang']= 'trim|required|max_length[200]');
 $fields['alamat']= 'trim|required|max_length[200]');
 $fields['kota']= 'trim|required|max_length[50]');
 $fields['phone']= 'trim|required|max_length[50]');
 $fields['fax']= 'trim|required|max_length[50]');
 $fields['email']= 'trim|required|max_length[50]');
 
 
 $this->validation->ser_rules($rules);
$this->validation->set_message('required','*required');
$this->validation->set_message('isset', '*required');
$this->validation->set_error_delimiters('<p class="error">', '</p>');
 }
    /** // run validation
        /**->form_validation->set_rules('cabang', 'cabang', 'required|max_length[200]');
		$this->form_validation->set_rules('alamat', 'alamat', 'required|max_length[200]');
		$this->form_validation->set_rules('kota', 'kota', 'required|max_length[50]');
		$this->form_validation->set_rules('phone', 'phone', 'required|max_length[50]');
		$this->form_validation->set_rules('fax', 'fax', 'required|max_length[50]');
		$this->form_validation->set_rules('email', 'email', 'required|max_length[50]');
		
		
		// Jika validasi sukses
		if ($this->form_validation->run() == TRUE)
		{
			// Persiapan data
			$cabang = array('cabang'		=> $this->input->post('cabang'),
							'alamat'		=> $this->input->post('alamat'),
							'kota'			=> $this->input->post('kota'),
							'phone'			=> $this->input->post('phone'),
							'fax'			=> $this->input->post('fax'),
							'email'			=> $this->input->post('email')
						);
			// Proses penyimpanan data di table cabang
			$this->Cabang_model->add($cabang);
		
			$this->session->set_flashdata('message', 'Satu data cabang berhasil disimpan!');
			redirect('cabang/add');
		}
		// Jika validasi gagal
		else
		{		
			$this->load->view('base', $data);
		}	
		function view($id_cabang)
	{
		$data['title'] 			= $this->title;
		$data['h2_title'] 		= 'Cabang > view';
		$data['main'] 		= 'cabang/cabang_view';
		$data['link'] 			= array('link_back' => anchor('cabang','kembali', array('class' => 'back'))
										);
	
		// cari data dari database
		$cabang = $this->Cabang_model->get_cabang_by_id($id_cabang);
				
		// buat session untuk menyimpan data primary key (id_cabang)
		$this->session->set_userdata('id_cabang', $cabang->id_cabang);
		
		// Data untuk mengisi field2 form	
		$data['default']['cabang']		= $cabang->cabang;		
		$data['default']['alamat']		= $cabang->alamat;		
		$data['default']['kota']		= $cabang->kota;		
		$data['default']['phone']		= $cabang->phone;		
		$data['default']['fax']			= $cabang->fax;		
		$data['default']['email']		= $cabang->email;
				
		$this->load->view('base', $data);
	}
	
	function update($id_cabang)
	{
		$data['title'] 			= $this->title;
		$data['h2_title'] 		= 'Cabang > Update';
		$data['main'] 		= 'cabang/cabang_form';
		$data['form_action']	= site_url('cabang/update_process');
		$data['link'] 			= array('link_back' => anchor('cabang','kembali', array('class' => 'back'))
										);
	
		// cari data dari database
		$cabang = $this->Cabang_model->get_cabang_by_id($id_cabang);
				
		// buat session untuk menyimpan data primary key (id_cabang)
		$this->session->set_userdata('id_cabang', $cabang->id_cabang);
		
		// Data untuk mengisi field2 form	
		$data['default']['cabang']		= $cabang->cabang;		
		$data['default']['alamat']		= $cabang->alamat;		
		$data['default']['kota']		= $cabang->kota;		
		$data['default']['phone']		= $cabang->phone;		
		$data['default']['fax']			= $cabang->fax;		
		$data['default']['email']		= $cabang->email;
				
		$this->load->view('base', $data);
	}
	
	/**
	 * Proses update data cabang
	 */
	/**function update_process()
	{
		$data['title'] 			= $this->title;
		$data['h2_title'] 		= 'Cabang > Update';
		$data['main'] 		= 'cabang/cabang_form';
		$data['form_action']	= site_url('cabang/update_process');
		$data['link'] 			= array('link_back' => anchor('cabang','kembali', array('class' => 'back'))
										);
										
		// Set validation rules
		$this->form_validation->set_rules('cabang', 'Cabang', 'required|max_length[200]');
		$this->form_validation->set_rules('alamat', 'alamat', 'required|max_length[200]');
		$this->form_validation->set_rules('kota', 'kota', 'required|max_length[50]');
		$this->form_validation->set_rules('phone', 'phone', 'required|max_length[50]');
		$this->form_validation->set_rules('fax', 'fax', 'required|max_length[50]');
		$this->form_validation->set_rules('email', 'email', 'required|max_length[50]');
		
		// Jika validasi sukses
		if ($this->form_validation->run() == TRUE)
		{
			// Persiapan data
			$cabang = array('cabang'		=> $this->input->post('cabang'),
							'alamat'		=> $this->input->post('alamat'),
							'kota'			=> $this->input->post('kota'),
						'phone'			=> $this->input->post('phone'),
							'fax'			=> $this->input->post('fax'),
							'email'			=> $this->input->post('email')
						);
			$this->Cabang_model->update($this->session->userdata('id_cabang'), $cabang);
			
			$this->session->set_flashdata('message', 'Satu data cabang berhasil diupdate!');
			redirect('cabang');
		}
		// Jika validasi gagal
		else
		{		
			$this->load->view('base', $data);
		}

	}

}*/

	/**
	 * Inisialisasi variabel untuk $title(untuk id element <body>)
	 */
	 /*
	var $title = 'Daftar cabang';
	
	/**
	 * Memeriksa user state, jika dalam keadaan login akan menampilkan halaman cabang,
	 * jika tidak akan meredirect ke halaman login
	 */
/**ction index()
	{
		if ($this->session->userdata('login') == TRUE)
		{
			if ($this->session->userdata('id_usergroup') == 6 || $this->session->userdata('id_usergroup') == 1 || $this->session->userdata('id_usergroup') == 2)
			{
				if($this->session->userdata('analisa') == TRUE){
				
					$this->analisa_model->ana_off();
					$this->get_all();
				}
				else {
					$this->get_all();
				}
			}
			else
			{
				redirect('home');
			}
		}
		else
		{
			redirect('login');
		}
	}
	
	/**
	 * Tampilkan semua data cabang
	 */
	/**ction get_all()
	{
		$data['title'] = $this->title;
		$data['h2_title'] = 'Cabang';
		$data['main'] = 'cabang/cabang';
		
		// Load data
		$query = $this->Cabang_model->get_all();
		$cabang = $query->result();
		$num_rows = $query->num_rows();
		
		if ($num_rows > 0)
		{
			
/**pl = array( 'table_open'    => '<table border="0" cellpadding="0" cellspacing="0">',
						  'row_alt_start'  => '<tr class="zebra">',
							'row_alt_end'    => '</tr>'
						  );
			$this->table->set_template($tmpl);
			/*Set table heading */
			/**is->table->set_empty("&nbsp;");
			$this->table->set_heading('No', 'Cabang', 'Kota', 'Actions');
			$i = 0;
			
			foreach ($cabang as $row)
			{
				$this->table->add_row(++$i, $row->cabang, $row->kota,
										anchor('cabang/view/'.$row->id_cabang,'view',array('class' => 'view')).' '.
										anchor('cabang/update/'.$row->id_cabang,'update',array('class' => 'update')).' '.
										anchor('cabang/delete/'.$row->id_cabang,'hapus',array('class'=> 'delete','onclick'=>"return confirm('Anda yakin akan menghapus data ini?')"))
										);
			}
			$data['table'] = $this->table->generate();
		}
		else
		{
			$data['message'] = 'Tidak ditemukan satupun data cabang!';
		}		
		
		$data['link'] = array('link_add' => anchor('cabang/add/','tambah data', array('class' => 'add'))
								);
		
		// Load view
		$this->load->view('base', $data);
	}
		
	/**
	 * Hapus data cabang
	 */
	/**ction delete($id_cabang)
	{
		$this->Cabang_model->delete($id_cabang);
		$this->session->set_flashdata('message', '1 data cabang berhasil dihapus');
		
		redirect('cabang');
	}
	
	/**
	 * Pindah ke halaman tambah cabang
	 */
	/**ction add()
	{		
		$data['title'] 			= $this->title;
		$data['h2_title'] 		= 'Tambah Cabang Koperasi';
		$data['main'] 		= 'cabang/cabang_form';
		$data['form_action']	= site_url('cabang/add_process');
		$data['link'] 			= array('link_back' => anchor('cabang','kembali', array('class' => 'back'))
										);
		
		$this->load->view('base', $data);
	}
	
	/**
	 * Proses tambah data cabang
	 */
	/**ction add_process()
	{
		$data['title'] 			= $this->title;
		$data['h2_title'] 		= 'Cabang > Tambah Data';
		$data['main'] 		= 'cabang/cabang_form';
		$data['form_action']	= site_url('cabang/add_process');
		$data['link'] 			= array('link_back' => anchor('cabang','kembali', array('class' => 'back'))
										);
	
		// Set validation rules
		$this->form_validation->set_rules('cabang', 'cabang', 'required|max_length[200]');
		$this->form_validation->set_rules('alamat', 'alamat', 'required|max_length[200]');
		$this->form_validation->set_rules('kota', 'kota', 'required|max_length[50]');
		$this->form_validation->set_rules('phone', 'phone', 'required|max_length[50]');
		$this->form_validation->set_rules('fax', 'fax', 'required|max_length[50]');
		$this->form_validation->set_rules('email', 'email', 'required|max_length[50]');
		
		
		// Jika validasi sukses
		if ($this->form_validation->run() == TRUE)
		{
			// Persiapan data
			$cabang = array('cabang'		=> $this->input->post('cabang'),
							'alamat'		=> $this->input->post('alamat'),
							'kota'			=> $this->input->post('kota'),
							'phone'			=> $this->input->post('phone'),
							'fax'			=> $this->input->post('fax'),
							'email'			=> $this->input->post('email')
						);
			// Proses penyimpanan data di table cabang
			$this->Cabang_model->add($cabang);
		
			$this->session->set_flashdata('message', 'Satu data cabang berhasil disimpan!');
			redirect('cabang/add');
		}
		// Jika validasi gagal
		else
		{		
			$this->load->view('base', $data);
		}	
	}
	
	/**
	 * Pindah ke halaman view cabang
	 */
	/**ction view($id_cabang)
	{
		$data['title'] 			= $this->title;
		$data['h2_title'] 		= 'Cabang > view';
		$data['main'] 		= 'cabang/cabang_view';
		$data['link'] 			= array('link_back' => anchor('cabang','kembali', array('class' => 'back'))
										);
	
		// cari data dari database
		$cabang = $this->Cabang_model->get_cabang_by_id($id_cabang);
				
		// buat session untuk menyimpan data primary key (id_cabang)
		$this->session->set_userdata('id_cabang', $cabang->id_cabang);
		
		// Data untuk mengisi field2 form	
		$data['default']['cabang']		= $cabang->cabang;		
		$data['default']['alamat']		= $cabang->alamat;		
		$data['default']['kota']		= $cabang->kota;		
		$data['default']['phone']		= $cabang->phone;		
		$data['default']['fax']			= $cabang->fax;		
		$data['default']['email']		= $cabang->email;
				
		$this->load->view('base', $data);
	}
	/**
	 * Pindah ke halaman update cabang
	 */
	/**ction update($id_cabang)
	{
		$data['title'] 			= $this->title;
		$data['h2_title'] 		= 'Cabang > Update';
		$data['main'] 		= 'cabang/cabang_form';
		$data['form_action']	= site_url('cabang/update_process');
		$data['link'] 			= array('link_back' => anchor('cabang','kembali', array('class' => 'back'))
										);
	
		// cari data dari database
		$cabang = $this->Cabang_model->get_cabang_by_id($id_cabang);
				
		// buat session untuk menyimpan data primary key (id_cabang)
		$this->session->set_userdata('id_cabang', $cabang->id_cabang);
		
		// Data untuk mengisi field2 form	
		$data['default']['cabang']		= $cabang->cabang;		
		$data['default']['alamat']		= $cabang->alamat;		
		$data['default']['kota']		= $cabang->kota;		
		$data['default']['phone']		= $cabang->phone;		
		$data['default']['fax']			= $cabang->fax;		
		$data['default']['email']		= $cabang->email;
				
		$this->load->view('base', $data);
	}
	
	/**
	 * Proses update data cabang
	 */
	/**ction update_process()
	{
		$data['title'] 			= $this->title;
		$data['h2_title'] 		= 'Cabang > Update';
		$data['main'] 		= 'cabang/cabang_form';
		$data['form_action']	= site_url('cabang/update_process');
		$data['link'] 			= array('link_back' => anchor('cabang','kembali', array('class' => 'back'))
										);
										
		// Set validation rules
		$this->form_validation->set_rules('cabang', 'Cabang', 'required|max_length[200]');
		$this->form_validation->set_rules('alamat', 'alamat', 'required|max_length[200]');
		$this->form_validation->set_rules('kota', 'kota', 'required|max_length[50]');
		$this->form_validation->set_rules('phone', 'phone', 'required|max_length[50]');
		$this->form_validation->set_rules('fax', 'fax', 'required|max_length[50]');
		$this->form_validation->set_rules('email', 'email', 'required|max_length[50]');
		
		// Jika validasi sukses
		if ($this->form_validation->run() == TRUE)
		{
			// Persiapan data
			$cabang = array('cabang'		=> $this->input->post('cabang'),
							'alamat'		=> $this->input->post('alamat'),
							'kota'			=> $this->input->post('kota'),
							'phone'			=> $this->input->post('phone'),
							'fax'			=> $this->input->post('fax'),
							'email'			=> $this->input->post('email')
						);
			$this->Cabang_model->update($this->session->userdata('id_cabang'), $cabang);
			
			$this->session->set_flashdata('message', 'Satu data cabang berhasil diupdate!');
			redirect('cabang');
		}
		// Jika validasi gagal
		else
		{		
			$this->load->view('base', $data);
		}

	}

}*/

// END cabang Class

/* End of file cabang.php */
/* Location: ./system/application/controllers/cabang.php */